package com.zhiche.lisa.bms.service.bill.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.bill.ExceptionBillDtlMapper;
import com.zhiche.lisa.bms.dao.model.bill.ExceptionBill;
import com.zhiche.lisa.bms.dao.model.bill.ExceptionBillDtl;
import com.zhiche.lisa.bms.pojo.vo.bill.BillExceptionVO;
import com.zhiche.lisa.bms.service.bill.ExceptionBillDtlService;
import com.zhiche.lisa.bms.service.bill.ExceptionBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 异常账单明细 服务实现类
 * </p>
 *
 * @author liuanshun
 * @since 2018-09-15
 */
@Service
public class ExceptionBillDtlServiceImpl extends ServiceImpl<ExceptionBillDtlMapper, ExceptionBillDtl> implements ExceptionBillDtlService {
	@Autowired
	private ExceptionBillService exceptionBillService;

	/**
	 * 新增异常账单明细
	 *
	 * @param exceptionBillList
	 */
	@Override
	public void insertExceptionBillDtl(List<ExceptionBillDtl> exceptionBillList) {
		this.insertBatch(exceptionBillList);
	}

	/**
	 * 新增异常账单
	 *
	 * @param exceptionBillList
	 * @param headId
	 */
	@Override
	public void insertExceptionBillDtl(List<ExceptionBillDtl> exceptionBillList, Long headId) {
		if (!CollectionUtils.isEmpty(exceptionBillList)) {
			exceptionBillList.forEach(exceptionBillDtl -> exceptionBillDtl.setHeadId(headId));
		}
		this.insertBatch(exceptionBillList);
	}

	/**
	 * 根据billId查询对应的详明列表
	 *
	 * @param billId
	 * @return
	 */
	@Override
	public List<BillExceptionVO> selectExceptionBillDtlByBillId(String billId) {
		EntityWrapper<ExceptionBill> exceptionBillEntityWrapper = new EntityWrapper<>();
		exceptionBillEntityWrapper.eq("bill_id", billId);
		List<ExceptionBill> exceptionBills = exceptionBillService.selectList(exceptionBillEntityWrapper);

		List<Long> headIds = new ArrayList<>();
		exceptionBills.forEach(exceptionBill -> {
			headIds.add(exceptionBill.getId());
		});
		if (CollectionUtils.isEmpty(headIds)) {
			return new ArrayList<>();
		}
		EntityWrapper<ExceptionBillDtl> exceptionBillDtlEntityWrapper = new EntityWrapper<>();
		exceptionBillDtlEntityWrapper.in("ed.head_id", headIds);
		return baseMapper.selectBillExceptionDtl(exceptionBillDtlEntityWrapper);
	}
}
